package krow.dev.gunpobus.view.fragment;

import java.util.ArrayList;
import java.util.List;

import krow.dev.gunpobus.Constants.Invoke;
import krow.dev.gunpobus.GlobalStore;
import krow.dev.gunpobus.NodeActivity;
import krow.dev.gunpobus.R;
import krow.dev.gunpobus.model.RouteItem;
import krow.dev.gunpobus.util.InitialSearcher;
import krow.dev.gunpobus.view.SearchViewWrapper;
import krow.dev.gunpobus.view.adapter.RouteListAdapter;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

public class RouteFragment extends BaseFragment {

	private Context mContext;
	
	private SearchViewWrapper mSearchViewWrapper;
	private RouteListAdapter mRouteListAdapter;
	private ListView mRouteListView;
	
	private List<RouteItem> mRouteItemList;
//	private Handler mHandler = new Handler();
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		mContext = inflater.getContext();
		
		View view = inflater.inflate(R.layout.fragment_route, null);

		mSearchViewWrapper = new SearchViewWrapper(mContext);
		mSearchViewWrapper.setHint(getString(R.string.hint_search_route));
		mSearchViewWrapper.addTextChangedListener(mKeywordTextWatcher);
		mSearchViewWrapper.setNumberOnly();
		mRouteListAdapter = new RouteListAdapter(mContext);
		mRouteListView = (ListView) view.findViewById(R.id.listview_route);
		mRouteListView.addHeaderView(mSearchViewWrapper.getView());
		mRouteListView.addFooterView(inflater.inflate(R.layout.footer_dummy, null));
		mRouteListView.setAdapter(mRouteListAdapter);
		mRouteListView.setOnItemClickListener(mItemClickListener);
		
		return view;
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		
		if(GlobalStore.getInstance().isAvailable()){
			requestRoute();
		}
	}

	@Override
	public void onResume() {
		super.onResume();

	}

	@Override
	public void onPause() {

		super.onPause();
	}
	
	@Override
	public void onDestroy() {
		
//		mHandler.removeCallbacks(mRequestFailureRunnable);
		
		super.onDestroy();
	}
	
	@Override
	protected void onRetry() {
		super.onRetry();
		
		requestRoute();
	}
	
	private void requestRoute(){
		
		mRouteItemList = GlobalStore.getInstance().getRouteItemList();
		mRouteListAdapter.setRouteItemList(mRouteItemList);
		mRouteListAdapter.notifyDataSetChanged();
		
//		setLoadBarVisibility(View.VISIBLE);
//		RequestManager.requestRoute(mResponseListener);
	}
	
	/*
	private HtmlResponseListener<RouteParser> mResponseListener = new HtmlResponseListener<RouteParser>() {

		@Override
		public void onResponse(RouteParser response) {
			
			setLoadBarVisibility(View.GONE);
			
			mRouteItemList = response.getRouteItemList();
			mRouteListAdapter.setRouteItemList(mRouteItemList);
			mRouteListAdapter.notifyDataSetChanged();
		}

		@Override
		public void onErrorResponse(VolleyError error) {
			
			mHandler.postDelayed(mRequestFailureRunnable, 1000);
		}
	};

	private Runnable mRequestFailureRunnable = new Runnable() {
		
		@Override
		public void run() {
			setLoadBarVisibility(View.GONE);
			mRouteListAdapter.clear();
			showRetryAlert();
		}
	};
	*/
	
	private TextWatcher mKeywordTextWatcher = new TextWatcher() {

		@Override
		public void onTextChanged(CharSequence s, int start, int before, int count) {

		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count, int after) {

		}

		@Override
		public void afterTextChanged(Editable s) {
			if (mRouteItemList == null) {
				return;
			}

			String keyword = s.toString();
			if (keyword.length() > 0) {
				search(keyword);
				
			}else{
				mRouteListAdapter.setRouteItemList(mRouteItemList);
			}
			mRouteListAdapter.notifyDataSetChanged();
		}
		
		private void search(String keyword){
			
			List<RouteItem> selectedItemList = new ArrayList<RouteItem>();

			for (RouteItem routeItem : mRouteItemList) {
				try {
					if(InitialSearcher.match(routeItem.getName(), keyword)){
						selectedItemList.add(routeItem);
					}
				} catch (Exception e) {
					
				}
			}
			
			mRouteListAdapter.setRouteItemList(selectedItemList);
		}
	};
	
	private AdapterView.OnItemClickListener mItemClickListener = new AdapterView.OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
			if(view.getTag() == null){
				return ;
			}
			
			RouteItem routeItem = (RouteItem) view.getTag();
			
			Intent intent = new Intent(getActivity(), NodeActivity.class);
			intent.putExtra(Invoke.KEY_ROUTE_NAME, routeItem.getName());
			intent.putExtra(Invoke.KEY_ROUTE_ID, routeItem.getId());
			intent.putExtra(Invoke.KEY_ROUTE_TYPE, routeItem.getType());
			startActivity(intent);
			getActivity().overridePendingTransition(R.anim.right_in, R.anim.scale_out);
		}
	};
}
